﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using 纸片人.CalculateControls;
using 纸片人.win2dEffectControl.DataClass;

namespace 纸片人.win2dEffectControl
{
    public abstract class TwoSourceEffectControl:EffectControl
    {
        protected ProviderDropControl twoproviderDropControl;

        #region 第二个源所需的数据
        protected ConnectLineUIElement twoConnectElement;
        #endregion

        public override EffectOneSourceData OneEffetData { get; set; }

        public abstract EffectTwoSourceData TwoEffectData { get; set; }

        public TwoSourceEffectControl()
        {
            this.DefaultStyleKey = typeof(TwoSourceEffectControl);
            
        }

        protected override void OnApplyTemplate()
        {
            OneEffetData = TwoEffectData;
            base.OnApplyTemplate();
            SetTwoproviderDropControlElement();
        }

        private void SetTwoproviderDropControlElement()
        {
            twoproviderDropControl = new ProviderDropControl() { AllowDrop = true, Background = new SolidColorBrush(Color.FromArgb(107,213,255,255)) };
            twoproviderDropControl.DragEnter += TwoproviderDropControl_DragEnter; ;
            twoproviderDropControl.Drop += TwoproviderDropControl_Drop;
            controlContainer.Children.Insert(controlContainer.Children.Count-1, twoproviderDropControl);

        }

        private void TwoproviderDropControl_Drop(object sender, Windows.UI.Xaml.DragEventArgs e)
        {
            var exporter = (EffectOutputControl)outputData.Exporter;


            if (IsChartLoop(exporter))
                return;
            CreateConnect( ref twoConnectElement, twoproviderDropControl);
            var exporterData = (EffectData)outputData.ExporterData;
            TwoEffectData.SetInputTwoEffectData(exporterData);
        }

     
        

        private void TwoproviderDropControl_DragEnter(object sender, Windows.UI.Xaml.DragEventArgs e)
        {
            if (outputData == null)
            {
                return;
            }
            e.AcceptedOperation = DataPackageOperation.Copy;
            e.DragUIOverride.Caption = "拖进来了";
        }

    

      

        /// <summary>
        /// 移除源
        /// </summary>
        public override  void RemoveExport(ConnectLineUIElement connectElement)
        {
            var exporter = (EffectOutputControl)connectElement.Exporter;
            var data = exporter.EffectData;
            if (connectElement== oneConnectElement)
            {
                oneConnectElement = null;
                OneEffetData.RemoveInputEffect(data);
            }
            else if(connectElement == twoConnectElement)
            {
                twoConnectElement = null;
                TwoEffectData.RemoveInputTwoEffect(data);
            }
            this.RemoveProvideroneConnectElement(connectElement);
        }

        public override void Destroy()
        {
            base.Destroy();
            TwoEffectData = null;
            twoConnectElement = null;
        }
    }
}
